package com.huobao.yunying.base;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;

import com.ab.activity.AbActivity;
import com.ab.view.titlebar.AbBottomBar;
import com.ab.view.titlebar.AbTitleBar;
import com.huobao.yunying.utils.PermissionsChecker;
import com.huobao.yunying.utils.Utils;
import com.huobao.zhangying.R;
import com.pgyersdk.crash.PgyCrashManager;
import com.xrz.sxm.base.dialog.BaseDialog;
import com.xrz.sxm.base.utils.ExitUtil;

import org.xutils.common.util.LogUtil;
import org.xutils.x;

public abstract class HuobaoBaseActivity extends AbActivity {

	protected HuobaoApplication application;
	protected Context context;

	protected AbTitleBar mAbTitleBar = null;
	protected AbBottomBar mAbBottomBar = null;

	protected Bundle savedInstanceState;

	private boolean check_tel,check_address,check_sdcard;

//	private PermissionsChecker mPermissionsChecker; // 权限检测器
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setAbContentView(getContentView());
		PgyCrashManager.register(this);
		this.savedInstanceState = savedInstanceState;
		x.view().inject(this);
		ExitUtil.getInstant().addToPool(this);
		application = (HuobaoApplication) abApplication;
		context = this;
		mAbTitleBar = this.getTitleBar();
		mAbTitleBar.setTitleBarGravity(Gravity.CENTER, Gravity.RIGHT);
		mAbBottomBar = this.getBottomBar();
		mAbTitleBar.setTitleText(getTitleText() == null ? "" : getTitleText());
		mAbTitleBar.setPadding(10, 0, 10, 0);
		mAbTitleBar.setGravity(Gravity.CENTER_VERTICAL);
		mAbTitleBar.getTitleTextButton().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
		mAbTitleBar.setVisibility(View.VISIBLE);
		mAbTitleBar.getTitleTextButton().setGravity(Gravity.CENTER_VERTICAL);
		changeQuanziTitle();
		mAbTitleBar.setLogoOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();   
			}
		});
		mAbTitleBar.getTitleTextButton().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		init();
	}

	@Override
	protected void onResume() {
		super.onResume();
//		requestPermission();
	}

	public void requestPermission(){
		try{
			int pid = android.os.Process.myPid();
			String processAppName = ((HuobaoApplication)getApplication()).getAppName(pid);
			if (processAppName != null && processAppName.equalsIgnoreCase("com.huobao.zhangying")) {
				if(!isCheckPermission())return;
				LogUtil.e("   =========================="+getLocalClassName());
				// 缺少权限时, 进入权限配置页面
				if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
					if(requestTelPermission()){
						if(requestAddressPermission()){
							if(requestSDcardPermission()){}
						}
					}
				}
			}
		}catch(Exception e){

		}
	}


	@Override
	protected void onStart() {
		super.onStart();

	}

	public boolean requestTelPermission(){
		if (!PermissionsChecker.lacksPermission(this,PermissionsChecker.PERMISSION_TEL)){
			return true;
		}
		BaseDialog.showPermissionDialog(this, getString(R.string.permission_tel), PermissionsChecker.PERMISSION_TEL,1, null);
		return false;
	}
	private boolean requestAddressPermission(){
		if (!PermissionsChecker.lacksPermission(this,PermissionsChecker.PERMISSION_ADDRESS)){
			return true;
		}
		BaseDialog.showPermissionDialog(this, getString(R.string.permission_address), PermissionsChecker.PERMISSION_ADDRESS,2, null);
		return false;
	}
	private boolean requestSDcardPermission(){
		if (!PermissionsChecker.lacksPermission(this,PermissionsChecker.PERMISSION_SDCARD)){
			return true;
		}
		BaseDialog.showPermissionDialog(this, getString(R.string.permission_sdcard), PermissionsChecker.PERMISSION_SDCARD,3, null);
		return false;
	}


	protected  boolean isCheckPermission(){
		return true;
	}


	protected void changeQuanziTitle(){
		mAbTitleBar.setLogo(R.drawable.selector_back);
		mAbTitleBar.setTitleBarBackgroundColor(Color.parseColor("#F8F8F8"));
		mAbTitleBar.getTitleTextButton().setTextColor(Color.parseColor("#333333"));
	}
	@Override
	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		if(requestCode == 1 && requestTelPermission()){
			Utils.checkUpdate(this, getCheckShowMsg());
			requestAddressPermission();
		}else if(requestCode == 2 && requestAddressPermission()){
			requestSDcardPermission();
		}
	}
	
	protected abstract int getContentView();  

	protected abstract String getTitleText();   

	protected abstract void init();

	@Override
	protected void onDestroy() {
		super.onDestroy();
		PgyCrashManager.unregister();
	}

	protected  boolean getCheckShowMsg(){
		return false;
	}


}
